package bbs2.service;

import javax.servlet.http.HttpServletRequest;

import bbs2.dao.LogDAO;
import bbs2.model.User;

public class LogService {

    public static void log(HttpServletRequest request, Exception e) {
        User user = UserService.getCurrentUser(request.getSession());
        int userID = (user == null) ? 0 : user.getId();
        String IP = request.getRemoteAddr();
        String URL = "";
        if (request.getAttribute("javax.servlet.error.request_uri") != null)
            URL = request.getAttribute("javax.servlet.error.request_uri") + "?" + request.getAttribute("javax.servlet.forward.query_string");
        else
            URL = request.getRequestURL().toString();
        LogDAO.insert(userID, IP, URL, e);
    }

    public static void log(Exception e) {
        LogDAO.insert(0, "", "", e);
    }
}
